<?php

class Application_Model_Mail extends Application_Model_DomainObject {

	/**
	 * 未读
	 * @var unknown_type
	 */
	const UNREAD=0;

	/**
	 * 已读
	 * @var unknown_type
	 */
	const READED=1;

	protected  $_id;

	protected $_userId;

	protected $_title;

	protected $_type;

	protected $_message;

	protected $_dateCreated;

	protected $_status;

	/**
	 * add by wuzw
	 */
	protected static function getDb(){
		return new Application_Model_DbTable_Mail();
	}

	/**
	 * 用分页的方式返回结果
	 * @param array $where 查询条件，如多个条件，第一个后面的请使用And
	 * @param string $orderBy 排序字段
	 * @param int $count 返回条数
	 * @param int $offset 从第几条数据开始返回结果
	 * @return arrry 返回格式为 array('size'=>?,'list'=>?,'pages'=>?,'index'=>?)
	 * size为查询记录的总条数，list为返回的array型数据，pages是指有多少页，index为当前是哪一页
	 */
	public static function  getMail($type,$userId,$start,$pageSize)
	{
		$where = array('userId'=>$userId);
		if ($type!=NULL)
		$where['type'] = $type;
			
		//edit by wuzw  change getDao() to getDb(); 
		return Application_Model_Mail::getDb()->queryPage($where,'dateCreated desc',$pageSize,$start);
	}

	public static function getUnRead($userId)
	{
		//edit by wuzw  change getDao() to getDb(); 		
		return Application_Model_Mail::getDb()->count(array('userId'=>$userId,'status'=>Application_Model_Mail::UNREAD));
	}
        public static function getselmail($nikename,$starttime,$endtime,$count,$offset)
        {
            $where = array();
            $and = '';
            if($nikename != ''){
                $result = Application_Model_User::getbynikename($nikename);
                if($result){
                    $where[$and.' userId = '] = $result->id;
                    $and = ' and ';
                }elseif($nikename=='群发消息'){
                    $where[$and.' userId = '] = 0;
                    $and = ' and ';
                }else{
                    return array();
                }
            }
            if($starttime != ''){
                $where[$and.' dateCreated >= '] = $starttime;
                $and = ' and ';
            }
            if($endtime != ''){
                $where[$and.' dateCreated <= '] = $endtime;
                $and = ' and ';
            }
            return Application_Model_Mail::getDao()->queryPage($where, ' id desc', $count, $offset);
        }
        public function getuserinfo()
        {
            return Application_Model_User::load($this->_userId);
        }
        
        public static function releasemgaAll($title,$type,$message,$content)
        {
            $mailItem = new Application_Model_Mail();
            $mailItem->title = $title;
            $mailItem->userId = 0;//userid=0 为群发所有人消息
            $mailItem->type = $type;
            $mailItem->message = $content;
            $mailItem->status = 0;
            $mailItem->dateCreated = date('Y-m-d H:i:s');
            $mailItem->save();
            return true;
        }
        
        public static function addMail($title,$type,$userId,$message)
        {
            $mailItem = new Application_Model_Mail();
            $mailItem->title = $title;
            $mailItem->userId = $userId;
            $mailItem->type = $type;
            $mailItem->message = $message;
            $mailItem->status = 0;
            $mailItem->dateCreated = date('Y-m-d H:i:s');
            $mailItem->save();
        }
                        
}